package org.ceva.training.util.pageNavigator;

import java.util.ArrayList;
import java.util.HashMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class NavigatorImpl implements Navigator {
	public int getEndRecord() {
		return currentPage*linkPerPage;
	}

	public int getStartRecord() {
		return this.startingPageNo;
	}

	private String pageLink;
	private int linkPerPage;
	private int startPage;
	private int endPage;
	private int startingPageNo;
	private int totalPages;
	private int currentPage;
	private int noOfRecords;
	private String divBlock;
	
	private String rightLink;
	private String leftLink;
	private HashMap pageLinkHash;
	private ArrayList pageLinksArray;
	
	private final Log logger = LogFactory.getLog(getClass());
	
	/*Constructor*/
	public NavigatorImpl(int linkPerPage) {
		logger.info("Page Per Link : " + linkPerPage);
		this.linkPerPage = linkPerPage;	
	}
	
	public int getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}
	public int getEndPage() {
		return endPage;
	}
	public void setEndPage(int endPage) {
		this.endPage = endPage;
	}
	public int getLinkPerPage() {
		return this.linkPerPage;
	}
	public void setLinkPerPage(int linkPerPage) {
		this.linkPerPage = linkPerPage;
	}
	public String getPageLink() {
		return pageLink;
	}
	public void setPageLink(String pageLink) {
		this.pageLink = pageLink;
	}
	public int getStartingPageNo() {
		return startingPageNo;
	}
	public void setStartingPageNo(int startingPageNo) {
		this.startingPageNo = startingPageNo;
	}
	public int getStartPage() {
		return startPage;
	}
	public void setStartPage(int startPage) {
		this.startPage = startPage;
	}
	public int getTotalPages() {
		return totalPages;
	}
	public void setTotalPages(int totalPages) {
		this.totalPages = totalPages;
	}
	
	public DisplayNavigator getHTML() {
		DisplayNavigator dn = new DisplayNavigator();
		
		int endRecords = currentPage*linkPerPage;
		if(endRecords>=noOfRecords)
			endRecords=noOfRecords;
		dn.setEndRecord(endRecords);
		
		dn.setStartRecord(startingPageNo+1);
		dn.setLeftLink(leftLink);
		dn.setRightLink(rightLink);
		dn.setPageNumberLink(pageLink);
		dn.setTotalRecords(noOfRecords);
		dn.setPageLinkHash(pageLinkHash);
		dn.setPageLinksArray(pageLinksArray);
		dn.setDivBlock(divBlock);
		if(noOfRecords > linkPerPage){
			dn.setDisplayNavigator(true);
			logger.info("Display navigator : true");
		}
		else{
			dn.setDisplayNavigator(false);
			logger.info("Display navigator : false");
		}		
		return dn;
	}

	public String genetarePageNav(int pageNo, int numOfRecord, String link) {
		this.noOfRecords=numOfRecord;
		logger.info("No of Records : " + this.noOfRecords);
		if(pageNo <=0)
            pageNo=1;
		this.currentPage=pageNo;
		this.startingPageNo=(pageNo-1) * this.linkPerPage;
		int pages =2;
        if(numOfRecord%this.linkPerPage == 0)
        {
        	pages = numOfRecord/this.linkPerPage;
        }
        else
            pages = (numOfRecord/this.linkPerPage) + 1;
            
        if(pages>0)
        {
            makepagelink(link, pageNo, pages);
        }
        else
        {
            pageLink = "1";
        }
        logger.info("Page Links : " + pageLink);
        
		return null;
	}

	private void makepagelink(String link, int pageNo, int pages) {
	
		int i,pagex;
		pageLinkHash = new HashMap ();
		
		logger.info("generating page links, pageNo "+ pageNo+", Total Pages : "+pages);
		//String rightLink = "&nbsp;&nbsp;<img src='"+ this.iconNextDisable +"' border=0><img src='"+ this.iconLastDisable +"'  border=0>";
		//String leftLink = "&nbsp;&nbsp;<img src='"+ this.iconFirstDisable +"' border=0><img src='"+ this.iconPreviousDisable +"'border=0>";				 
		pageLinksArray = new ArrayList();
		if(pageNo!=0)
		{
			if( pageNo != 1)
			{
				pageLinkHash.put("go2previous", link +"&page="+(pageNo-1)+"&totalRecords="+this.getNoOfRecords());
				pageLinkHash.put("go2first", link+"&page=1&totalRecords="+this.getNoOfRecords());
			}
			if(pageNo>=6)
			{
				pageLinkHash.put("nextLinks",link+"&page="+(pageNo-5)+"&totalRecords="+this.getNoOfRecords());
			}
			if((pageNo+4)>=pages)
			{	
				pagex=pages;
			}
			else
			{
				pagex=pageNo+4;
			}
			for( i=pageNo-4 ; i<=pagex ; i++) 
			{
				ArrayList pagelink=new ArrayList();
				
				logger.info("Page Links Generated "+i);
				if(i<=0)
					i=1;
				pagelink.add(Integer.valueOf(i));
				if(i==pageNo)
				{	
					pagelink.add("NA");
				}
				else
				{
					pagelink.add(link+"&page="+ i +"&totalRecords="+this.getNoOfRecords());
				}	
				pageLinksArray.add(pagelink);
			}
			
			if((pages-pageNo)>=5)
			{
				pageLinkHash.put("nextLinks", link+"&page="+(pageNo+5)+"&totalRecords="+this.getNoOfRecords());
			}
			if(pageNo!=pages) 
			{	
				pageLinkHash.put("go2next", link +"&page="+(pageNo+1)+"&totalRecords="+this.getNoOfRecords());
				pageLinkHash.put("go2last", link+ "&page=" +pages+"&totalRecords="+this.getNoOfRecords());
			}
		}  
		
	}
	
	public int getNoOfRecords() {
		return noOfRecords;
	}


	public String getDivBlock() {
		return divBlock;
	}

	public void setDivBlock(String divBlock) {
		this.divBlock = divBlock;
	}

	
}
